package cn.csc.module.shop.service.recommend;

import cn.csc.framework.common.pojo.PageResult;
import cn.csc.module.shop.controller.admin.recommend.vo.*;
import cn.csc.module.shop.convert.recommend.ShopRecommendConvert;
import cn.csc.module.shop.dal.dataobject.recommend.ShopRecommendDO;
import cn.csc.module.shop.dal.mysql.recommend.ShopRecommendMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.csc.module.shop.enums.ErrorCodeConstants.RECOMMEND_NOT_EXISTS;

/**
 * 店铺推荐记录 Service 实现类
 *
 * @author liuchuang
 */
@Service
@Validated
public class ShopRecommendServiceImpl implements ShopRecommendService {

    @Resource
    private ShopRecommendMapper recommendMapper;

    @Override
    public BigInteger createRecommend(ShopRecommendCreateReqVO createReqVO) {
        // 插入
        ShopRecommendDO recommend = ShopRecommendConvert.INSTANCE.convert(createReqVO);
        recommendMapper.insert(recommend);
        // 返回
        return recommend.getId();
    }

    @Override
    public void updateRecommend(ShopRecommendUpdateReqVO updateReqVO) {
        // 校验存在
        validateRecommendExists(updateReqVO.getId());
        // 更新
        ShopRecommendDO updateObj = ShopRecommendConvert.INSTANCE.convert(updateReqVO);
        recommendMapper.updateById(updateObj);
    }

    @Override
    public void deleteRecommend(BigInteger id) {
        // 校验存在
        validateRecommendExists(id);
        // 删除
        recommendMapper.deleteById(id);
    }

    @Override
    public Integer deleteRecommendByShopId(BigInteger shopId) {
        // 删除
        return recommendMapper.delete(new QueryWrapper<ShopRecommendDO>().eq("shop_id", shopId));
    }

    private void validateRecommendExists(BigInteger id) {
        if (recommendMapper.selectById(id) == null) {
            throw exception(RECOMMEND_NOT_EXISTS);
        }
    }

    @Override
    public ShopRecommendDO getRecommend(BigInteger id) {
        return recommendMapper.selectById(id);
    }

    @Override
    public List<ShopRecommendDO> getRecommendList(Collection<BigInteger> ids) {
        return recommendMapper.selectBatchIds(ids);
    }

    @Override
    public List<ShopRecommendDO> getRecommendListOfLatest(ShopRecommendListReqVO reqVO) {
        return recommendMapper.getRecommendListOfLatest(reqVO);
    }

    @Override
    public PageResult<ShopRecommendDO> getRecommendPage(ShopRecommendPageReqVO pageReqVO) {
        return recommendMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ShopRecommendDO> getRecommendList(ShopRecommendExportReqVO exportReqVO) {
        return recommendMapper.selectList(exportReqVO);
    }

}
